package com.ben.notebook.domain;

import com.ben.notebook.infrastructure.mapper.NoteMapper;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 备忘录实体
 * 永远注意要将数据和行为一起看待
 * 创建新页面和进入修改页面只是前端操作，只有保存和删除是修改数据
 * @date 2020-11-22
 */
public class Note {

    private Long noteId;

    private String title;

    private String tag;

    private String content;

    //枚举：草稿，已发布，已删除
    private Character status;

    private LocalDateTime createTime;

    private LocalDateTime modifyTime;

    /**
     * 此备忘录对象被发布，新增发布
     */
    public int publish(NoteMapper mapper){
        this.setStatus(NoteStatusEnum.PUBLISHED.getValue());
        this.setCreateTime(LocalDateTime.now());
        return mapper.create(this);
    }

    /**
     * 此备忘录对象被修改，修改发布
     */
    public int update(NoteMapper mapper) {
        this.setStatus(NoteStatusEnum.PUBLISHED.getValue());
        this.setModifyTime(LocalDateTime.now());
        return mapper.update(this);
    }

    /**
     * 此备忘录对象被删除，仅修改状态
     */
    public int delete(NoteMapper mapper){
        this.setStatus(NoteStatusEnum.DELETED.getValue());
        return mapper.deleteByChangeStatus(this);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Note note = (Note) o;
        return Objects.equals(noteId, note.noteId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(noteId, title, tag, content, status, createTime, modifyTime);
    }

    public Long getNoteId() {
        return noteId;
    }

    public void setNoteId(Long noteId) {
        this.noteId = noteId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    public LocalDateTime getModifyTime() {
        return modifyTime;
    }

    public void setModifyTime(LocalDateTime modifyTime) {
        this.modifyTime = modifyTime;
    }

    public Character getStatus() {
        return status;
    }

    public void setStatus(Character status) {
        this.status = status;
    }
}
